package cn.csc.module.shoppingCard.dal.mysql.project;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.mybatis.core.mapper.BaseMapperX;
import cn.csc.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.csc.module.shoppingCard.controller.admin.project.vo.CardProjectExportReqVO;
import cn.csc.module.shoppingCard.controller.admin.project.vo.CardProjectPageReqVO;
import cn.csc.module.shoppingCard.dal.dataobject.project.CardProjectDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物卡项目 Mapper
 *
 * @author liuchuang
 */
@Mapper
public interface CardProjectMapper extends BaseMapperX<CardProjectDO> {

    default PageResult<CardProjectDO> selectPage(CardProjectPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CardProjectDO>()
                .likeIfPresent(CardProjectDO::getName, reqVO.getName())
                .eqIfPresent(CardProjectDO::getFaceValue, reqVO.getFaceValue())
                .eqIfPresent(CardProjectDO::getTotalNum, reqVO.getTotalNum())
                .eqIfPresent(CardProjectDO::getActivateNum, reqVO.getActivateNum())
                .betweenIfPresent(CardProjectDO::getExpireDate, reqVO.getExpireDate())
                .betweenIfPresent(CardProjectDO::getCreateTime, reqVO.getCreateTime())
                .ge(reqVO.getStatus() != null && reqVO.getStatus() == 1 , CardProjectDO::getExpireDate, LocalDateTime.now())
                .lt(reqVO.getStatus() != null && reqVO.getStatus() == 0, CardProjectDO::getExpireDate, LocalDateTime.now())
                .orderByDesc(CardProjectDO::getCreateTime));
    }

    default List<CardProjectDO> selectList(CardProjectExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<CardProjectDO>()
                .likeIfPresent(CardProjectDO::getName, reqVO.getName())
                .eqIfPresent(CardProjectDO::getFaceValue, reqVO.getFaceValue())
                .eqIfPresent(CardProjectDO::getTotalNum, reqVO.getTotalNum())
                .eqIfPresent(CardProjectDO::getActivateNum, reqVO.getActivateNum())
                .betweenIfPresent(CardProjectDO::getExpireDate, reqVO.getExpireDate())
                .betweenIfPresent(CardProjectDO::getCreateTime, reqVO.getCreateTime())
                .ge(reqVO.getStatus() != null && reqVO.getStatus() == 1 , CardProjectDO::getExpireDate, LocalDateTime.now())
                .lt(reqVO.getStatus() != null && reqVO.getStatus() == 0, CardProjectDO::getExpireDate, LocalDateTime.now())
                .orderByDesc(CardProjectDO::getId));
    }


    default List<CardProjectDO> selectList(){
        return selectList(new LambdaQueryWrapperX<CardProjectDO>()
                .select(CardProjectDO::getId, CardProjectDO::getName)
        );
    }

    /**
     * 更新激活数
     */
    default int updateActivateNumById(BigInteger id, Integer activateNum) {
        LambdaUpdateWrapper<CardProjectDO> updateWrapper = new LambdaUpdateWrapper<>();

        updateWrapper.set(CardProjectDO::getActivateNum, activateNum);
        updateWrapper.set(CardProjectDO::getUpdateTime, LocalDateTime.now());
        updateWrapper.eq(CardProjectDO::getId, id);
        return update(null, updateWrapper);
    }


}
